﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DB.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace WPF_for_Auth.ViewModel
{
    /// <summary>
    /// 主打当前的状态，着重记录你是谁。
    /// 当知道当前用户是谁时，才能做出更好的决策。
    /// 2025年6月27日09:52:56
    /// </summary>
    internal partial class NowStatus : ObservableObject
    {
        /// <summary>
        /// 当前登录用户信息
        /// </summary>
        [ObservableProperty]
        DB.Models.RegistUser? nowUser = null;


        private readonly MvvmLoad _mvvmLoad;
        public NowStatus(MvvmLoad mvvmLoad)
        {
            // 这里可以传入 MvvmLoad 实例以便访问其他状态或方法
            _mvvmLoad = mvvmLoad ?? throw new ArgumentNullException(nameof(mvvmLoad));

            // 初始化时可以设置默认值或执行其他逻辑
            NowUser = null;//咱们暂时先用这个空白来表示是未登录状态
        }
        /// <summary>
        /// 登录和退出操作逻辑
        /// </summary>
        [RelayCommand]
        public void LoginOrLogout()
        {
            if (NowUser == null)
            {
                // 执行登录逻辑
                // 这里可以调用登录窗口或其他方法来处理登录
                MessageBox.Show("执行登录逻辑");
                using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
                {
                    // 假设登录成功后获取用户信息
                    // 这里可以替换为实际的登录逻辑
                    NowUser = db.RegistUsers
                        .Include(o=>o.UserDetails)
                        .Include(o=>o.Department)
                        .Include(o=>o.Role).ThenInclude(r=>r.RoleComparePageControls)
                        .FirstOrDefault(u => u.LoginName == "admin"); // 示例：获取名为 "testuser" 的用户

                    var mima = DB.Utils.Tools.NewHsha256("admin");
                    //MessageBox.Show($"登录成功，当前用户：{NowUser?.LoginName ?? "未知用户"}");
                }
            }
            else
            {
                var result= MessageBox.Show($"当前用户【{NowUser.LoginName}】，是否退出？", "友情提示",MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    // 执行登出逻辑
                    NowUser = null; // 清空当前用户信息
                    _mvvmLoad.App_PageStatus.CurrentPage = null;
                    MessageBox.Show("执行登出逻辑");
                }

            }
        }
        /// <summary>
        /// 点击用户名称按钮操作逻辑
        /// </summary>
        [RelayCommand]
        public void ShowUserDetails()
        {
            if (NowUser != null)
            {
                
                MessageBox.Show($"当前用户【{NowUser.LoginName}】", "用户详细信息");
            }
            else
            {
                MessageBox.Show("当前没有登录用户", "提示");
            }
        }
    }
    /// <summary>
    /// 根据当前用户，显示一下用户名称。
    /// </summary>
    internal class NowUserTextConvertHelper : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is RegistUser)
            {
                var temp = value as RegistUser;
                if (temp == null)
                {
                    return "游客未登录";
                }
                else
                {
                    return temp.LoginName;
                }
                    
            }
            else
            {
                return "游客";
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// 根据当前是否登录，显示退出/登录 按钮的文字
    /// </summary>
    internal class LoginOrLogoutButtonTextConvertHelper : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is RegistUser)
            {
                return "退出";
            }
            else
            {
                return "登录";
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
